package cn.datawin.bms.modules.provider.service.impl;

import cn.datawin.bms.modules.provider.mapper.ProvProviderChannelMapper;
import cn.datawin.bms.modules.provider.service.ProvCmppClientInfoService;
import cn.datawin.bms.modules.provider.service.ProvProviderChannelService;
import cn.datawin.bms.modules.provider.service.ProvProviderParamService;
import cn.datawin.bms.modules.provider.service.ProvProviderService;
import cn.datawin.bms.provider.bo.ProvProviderBo;
import cn.datawin.bms.provider.entity.ProvCmppClientInfoEntity;
import cn.datawin.bms.provider.entity.ProvProviderChannelEntity;
import cn.datawin.bms.provider.redis.ProvProviderChannelRedisDto;
import cn.datawin.common.core.util.RedisUtils;
import cn.datawin.constant.RedisPattern;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 供应商通道
 *
 * @author datawin-sms
 * @date 2025-08-22 11:24:59
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProvProviderChannelServiceImpl extends ServiceImpl<ProvProviderChannelMapper, ProvProviderChannelEntity> implements ProvProviderChannelService {


	private final ProvProviderParamService provProviderParamService;

	private final ProvCmppClientInfoService cmppClientInfoService;

    @Override
    public boolean saveChannel(ProvProviderChannelEntity entity) {
		boolean b = SqlHelper.retBool(baseMapper.insert(entity));
		if(b){
			//插入通道信息到缓存中
			dataToCache(entity.getId());
		}
		return b;
    }

	@Override
	public boolean updateChannel(ProvProviderChannelEntity provProviderChannel) {
		boolean b = SqlHelper.retBool(baseMapper.updateById(provProviderChannel));
		if(b){
			//更新通道信息到缓存中
			dataToCache(provProviderChannel.getId());
		}
		return false;
	}

	public void dataToCache(Long id) {
		ProvProviderChannelEntity channel = baseMapper.selectById(id);


		String rKey = String.format(RedisPattern.PROV_PROVIDER_CHANNEL_CACHE, id);
		ProvProviderChannelRedisDto redisDto = new ProvProviderChannelRedisDto();
		BeanUtil.copyProperties(channel,redisDto);
		//通道参数
		Map<String,String> providerParams = provProviderParamService.getParams(channel.getProviderId());
		if(CollUtil.isNotEmpty(providerParams)){
			redisDto.setProviderParams(providerParams);
		}
		ProvProviderBo provider = SpringUtil.getBean(ProvProviderService.class).getProviderById(channel.getProviderId());

		if(provider!=null){
			Long cmppClientId = provider.getClientInfoId();
			if(cmppClientId!=null){
				//服务器信息
				ProvCmppClientInfoEntity cmppClientInfo = cmppClientInfoService.getById(cmppClientId);
				if(cmppClientInfo != null){
					providerParams.put("pkey",cmppClientInfo.getPkey());
				}
			}
		}

		RedisUtils.set(rKey, redisDto);
		//测试查询
		ProvProviderChannelRedisDto dto = RedisUtils.get(rKey);
		log.info("测试查询:{}",dto);
	}
}
